/**
 * JAVACC DEMO 1.0
 * @copy right apache company All rights reserved. 
 * @Package com.apache.task.impl  
 */
package com.apache.portal.task;

import com.apache.database.model.TaskEntity;
import com.apache.rpc.common.LoadRpcService;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.tools.ConfigUtil;
import com.apache.tools.OsUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * description:  实现Runable接口，线程池中调用
 * @author Hou Dayu 创建时间：2016-1-14
 * @Copyright(c)2014:
 */
public class TaskRemoteRunnable implements Runnable {

	private TaskEntity task;
	private Logger log = LoggerFactory.getLogger("taskServer");
	InterfaceRegister register;

	public TaskRemoteRunnable() {
	}

	public TaskRemoteRunnable(TaskEntity task) {
		this.task = task;
		register =new InterfaceRegister();
		register.setAddress("127.0.0.1");
		register.setCallType("socket");
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see Runnable#run()
	 */
	public void run() {
		try {
			int count = Calendar.getInstance().getTime().getHours();
			boolean flag = false;
			if (0 == task.getStartTime() && 0 == task.getEndTime()) {
				flag = true;
			} else if (0 == task.getStartTime() && 0 < task.getEndTime()) {
				flag = count <= Integer.valueOf(task.getEndTime());
			} else if (0 < task.getStartTime() && 0 < task.getEndTime()) {
				flag = task.getStartTime() <= count && count <= task.getEndTime();
			}
			if (flag) {
				Map<String,Object> params = new HashMap<String,Object>();
				params.put("sysPass", ConfigUtil.getInstance().interfacePass());
				params.put("Client-IP", OsUtils.getLocalIp());
				LoadRpcService
                        .service().doServiceClient(task.getClassName(),task.getMethodName(),params,register);
			}
		} catch (Exception e) {
			log.error("定时任务执行("+task.getClassName()+":"+task.getMethodName()+")出错："+e.getMessage());
		}
	}

}
